package cn.iocoder.yudao.module.system.service.tenantapprovelist;

import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.yudao.module.system.service.tenant.TenantService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.tenantapprovelist.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.tenantapprovelist.TenantApproveListDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.tenantapprovelist.TenantApproveListMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 开户申请记录 Service 实现类
 *
 * @author 数创万维
 */
@Service
@Validated
public class TenantApproveListServiceImpl implements TenantApproveListService {

    @Resource
    private TenantApproveListMapper tenantApproveListMapper;

    @Resource
    private TenantService tenantService;

    @Override
    public Long createTenantApproveList(TenantApproveListSaveReqVO createReqVO) {
        // 插入
        TenantApproveListDO tenantApproveList = BeanUtils.toBean(createReqVO, TenantApproveListDO.class);
        tenantApproveListMapper.insert(tenantApproveList);
        // 返回
        return tenantApproveList.getId();
    }

    @Override
    public void updateTenantApproveList(TenantApproveListSaveReqVO updateReqVO) {
        // 校验存在
//        validateTenantApproveListExists(updateReqVO.getId());
        // 更新
        TenantApproveListDO updateObj = BeanUtils.toBean(updateReqVO, TenantApproveListDO.class);
        tenantApproveListMapper.updateById(updateObj);
    }

    @Override
    public void deleteTenantApproveList(Long id) {
        // 校验存在
//        validateTenantApproveListExists(id);
        // 删除
        tenantApproveListMapper.deleteById(id);
    }

//    private void validateTenantApproveListExists(Long id) {
//        if (tenantApproveListMapper.selectById(id) == null) {
//            throw exception(TENANT_APPROVE_LIST_NOT_EXISTS);
//        }
//    }

    @Override
    public TenantApproveListDO getTenantApproveList(Long id) {
        return tenantApproveListMapper.selectById(id);
    }

    @Override
    public PageResult<TenantApproveListDO> getTenantApproveListPage(TenantApproveListPageReqVO pageReqVO) {
        return tenantApproveListMapper.selectPage(pageReqVO);
    }

    @Override
    public void approve(TenantApproveListApproveReqVO createReqVO) {
        TenantApproveListDO tenantApproveList = BeanUtils.toBean(createReqVO, TenantApproveListDO.class);
        tenantApproveListMapper.updateById(tenantApproveList);
    }

    @Override
    public void handleCreate(Long id) {
        TenantApproveListDO approveData = getTenantApproveList(id);
        if (Objects.nonNull(approveData.getCreateTenantId())) {
            throw exception(new ErrorCode(100500, "不能重复创建租户"));
        }
        TenantDO tenant = tenantService.getTenant(approveData.getTenantId());
        @Valid TenantSaveReqVO tenantSaveReqVO = new TenantSaveReqVO();
        // 客户名称
        tenantSaveReqVO.setName(approveData.getCorpName());
        // 客户类型 1-普通租户
        tenantSaveReqVO.setBizType(1);
        tenantSaveReqVO.setContactName(approveData.getContactName());
        tenantSaveReqVO.setContactMobile(approveData.getContactMobile());
        tenantSaveReqVO.setWebsite(tenant.getWebsite());
        tenantSaveReqVO.setPackageId(2L);
        tenantSaveReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
//        tenantSaveReqVO.setExpireTime(DateUtil.offsetMonth(new Date(), 12).toLocalDateTime());
        tenantSaveReqVO.setParentId(tenant.getId());
        tenantSaveReqVO.setUsername(approveData.getAdminUser());
        tenantSaveReqVO.setPassword(approveData.getAdminUserPass());
        tenantSaveReqVO.setCallCostPrice(approveData.getCallCostPrice());
        tenantSaveReqVO.setCallSalePrice(approveData.getCallSalePrice());

        Map<String, Long> tenant1 = tenantService.createTenant(tenantSaveReqVO);

        approveData.setHandleStatus(1);
        approveData.setOperator(getLoginUserId());
        approveData.setCreateTenantId(tenant1.get("tenantId"));
        tenantApproveListMapper.updateById(approveData);

    }
}